﻿namespace HZY.Shared.Admin.Extensions.Dict;

/// <summary>
/// 字典信息
/// </summary>
public class DictInfo
{
    public DictInfo(DictAttribute dict, List<object?> data, PropertyInfo propertyInfo)
    {
        Dict = dict;
        Data = data;
        PropertyInfo = propertyInfo;
    }

    public DictAttribute Dict { get; set; }

    public List<object?> Data { get; set; }

    public PropertyInfo PropertyInfo { get; set; }

    public string Name => PropertyInfo.Name;

    public void SetData(object? value)
    {
        Data.Add(value);
    }

    private List<DictDataSource> DataSource { get; set; } = [];

    public List<DictDataSource> BuildDataSource(IFreeSql freeSql)
    {
        if (DataSource.Count > 0)
        {
            return DataSource;
        }

        var dictTableName = Dict.DictTableName;
        var dictCode = Dict.DictCode;
        var dictName = Dict.DictName;

        // 生成 sql 查询数据
        var sql = $"SELECT {dictCode} as Code,{dictName} as Name FROM {dictTableName} WHERE {dictCode} IN ({string.Join(",", Data)})";

        // 查询数据
        DataSource = freeSql.Ado.Query<DictDataSource>(sql);

        return DataSource;
    }



}